<template>
  <el-dialog :close-on-click-modal='false' v-drag :title="$t(title)" :v-if="dialogVisible" :visible.sync="dialogVisible" width="375px" @close='close' top="20vh">
    <div class="dialogContentBox">
      <el-form ref="form" :model="record" label-width="100px" label-position="left" :rules="rules" style="margin:0 20px;">
        <el-form-item :label="$t('网口')" prop="PortName">
          <el-select v-model="record.PortName" ref="PortName" :disabled="title !== '添加'" clearable size="small">
            <el-option v-for="item in portNameList" :disabled="item.enPortMode === 3" :key="item.name" :label="item.name" :value="item.name"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item :label="$t('子网前缀')" prop="Prefix">
          <el-input v-emoji v-model="record.Prefix" ref="Prefix" :placeholder="$t('示例：2025::0/64')" size='small'></el-input>
        </el-form-item>
      </el-form>
    </div>
    <span slot="footer" class="dialog-footer">
      <el-button type="primary" @click="submit" class="addSubmit">{{$t('确 定')}}</el-button>
      <el-button @click="close" class="addClose">{{$t('取 消')}}</el-button>
    </span>
  </el-dialog>
</template>
<script>
import { addRadvdInfo, editRadvdInfo } from '@/api/network/route/radvd'
import { getPhysicalApiArray } from '@/api/network/api/physical'
import { inputClass, selectClass } from '@/utils/input-class.js'
// import { getVirtualApiArray } from '@/api/network/api/virtual'
export default {
  name: 'RadvdDialog',
  data () {
    var validateName = (rule, value, callback) => {
      if (String(value).trim() === '') {
        callback(new Error(this.$t('不能为空')))
      } else {
        callback()
      }
    }

    return {
      dialogVisible: false,
      title: '添加',
      record: {
        PortName: '',
        Prefix: ''
      },
      recordCopy: {
        PortName: '',
        Prefix: ''
      },
      portNameList: [],
      rules: {
        PortName: [{ required: false, validator: validateName, trigger: 'blur' }]
      }
    }
  },
  methods: {
    init (data) {
      this.dialogVisible = true
      this.portNameList = []
      this.$nextTick(() => {
        const data = ['Prefix']
        const selectData = ['PortName']
        selectClass(this.$refs, selectData)
        inputClass(this.$refs, data)
      })
      getPhysicalApiArray().then(res => { // strPhysicsName
        if (res.status === 0) {
          res.info.forEach((item, index) => {
            this.portNameList.push({ name: item.strPhysicsName, enPortMode: item.enPortMode })
          })
          // getVirtualApiArray().then(res1 => { // strBridgeName
          //   if (res.status === 0) {
          //     res1.info.forEach((item, index) => {
          //       this.portNameList.push({ name: item.strBridgeName, enPortMode: item.enPortMode })
          //     })
          //   }
          // })
        }
      })
      if (data) {
        this.title = '编辑'
        this.record = Object.assign(this.record, data)
      }
    },
    submit () {
      this.$refs.form.validate((valid) => {
        if (valid) {
          if (this.title === '添加') {
            addRadvdInfo(this.record).then(res => {
              if (res.status === 0) {
                this.$message({
                  message: this.$t('添加成功'),
                  type: 'success'
                })
                this.close()
                this.$parent.listData()
              }
            })
          } else {
            editRadvdInfo(this.record).then(res => {
              if (res.status === 0) {
                this.$message({
                  message: this.$t('编辑成功'),
                  type: 'success'
                })
                this.close()
                this.$parent.listData()
              }
            })
          }
        }
      })
    },
    close () {
      this.dialogVisible = false
      this.record = Object.assign({}, this.recordCopy)
      this.$refs.form.resetFields()
      this.title = '添加'
      this.portNameList = []
    }
  }
}
</script>
<style scoped>
.dialogContentBox {
  max-height: calc(60vh - 184px);
  overflow-y: auto;
}
</style>
